import { DataModel, MsgModel } from "../../until/context";
class DeviceService { 
    async getModuleNum() {
        let maxId = await DataModel.max("module_id")
        return maxId
    }

      // 获取监测点的报警次数排行
    async getSortPoint(gatewayIp?: string) {
        let whereCondition: any = {};
        if (gatewayIp) {
            whereCondition.gateway_ip = gatewayIp;
        }

        let res = await MsgModel.count({
            where: whereCondition,
            group: ["module_id"]
        })
        let result = [...res]
        //@ts-ignore
        let id_list:number[] = res.map(item => item.module_id)
        //@ts-ignore
        if (port.moduleNum > 10) {
            //@ts-ignore
            for (let i = 0; i < 10; i++) {
                if (id_list.includes(i+1)) continue
                result.push({
                    module_id: i + 1,
                    count: 0
                })
            }
        } else {
            //@ts-ignore
            for (let i = 0; i <port.moduleNum; i++) {
                if (id_list.includes(i+1)) continue
                result.push({
                    module_id: i + 1,
                    count: 0
                })
            }
        }

       return result.sort((a, b) => b.count - a.count)
    }
}

export default new DeviceService();